#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：turbo 
@File    ：handlers.py
@Author  ：赵加响
@Date    ：2023/4/7 13:50 
@Desc    ：
"""
from apps.rbac.models import Menu, MenuOperate, RoleMenuOperate, Role, MenuSensitiveField
from constants import LANGUAGE_ZH


def generate_menu_tree(menu_list, parent, checked_list, role_id, localization):
    """
    递归方法
    1、获取全部一级菜单（Green Light=True, is_active=True, parent is null）
    2、遍历1获取的数据
        1、判断是否存在子菜单
            1、无子菜单 -> 获取菜单的所有
    """
    parent_menus = Menu.objects.filter(is_green_light=False, is_active=True, parent=parent).order_by('sort_rule',
                                                                                                     'code')

    for parent_menu in parent_menus:
        parent_dict = dict()
        parent_dict['id'] = parent_menu.code
        parent_dict['label'] = parent_menu.name if localization == LANGUAGE_ZH else parent_menu.name_en

        children_list = []
        parent_dict['children'] = children_list

        children_menus = Menu.objects.filter(is_green_light=False, is_active=True, parent=parent_menu).order_by(
            'sort_rule', 'code')

        if children_menus:
            generate_menu_tree(children_list, parent_menu, checked_list, role_id, localization)
            menu_list.append(parent_dict)
        else:
            menu_operates = MenuOperate.objects.filter(menu=parent_menu)
            for menu_operate in menu_operates:
                operate_dict = dict()
                operate_dict['id'] = menu_operate.id
                operate_dict['label'] = \
                    menu_operate.operate.name if localization == LANGUAGE_ZH else menu_operate.operate.name_en
                children_list.append(operate_dict)

                role_menu_operate = RoleMenuOperate.objects.filter(is_active=True, role_id=role_id,
                                                                   menu_operate=menu_operate)
                if role_menu_operate:
                    checked_list.append(menu_operate.id)
            # 是否可以操作，适应前端tree组件横向显示
            parent_dict['is_operate'] = True
            menu_list.append(parent_dict)


def generate_role_tree(role_dict, parent_id, localization):
    """
    递归方法
    1、根据父角色ID获取所有子角色
    """
    roles = Role.objects.filter(parent=parent_id)
    son_role_list = []
    for role in roles:
        son_role_dict = dict()
        son_role_dict['id'] = role.id
        son_role_dict['name'] = role.name_zh if localization == LANGUAGE_ZH else role.name_en
        son_role_list.append(son_role_dict)
        role_dict['children'] = son_role_list
        generate_role_tree(son_role_dict, role.id, localization)


def generate_sensitive_menu_structure_tree(sensitive_menu_list, sensitive_parent, localization):
    """
    递归方法获取菜单的构成
    """
    menu_id_list = None

    def _run(menu_list, parent):
        parent_menus = Menu.objects.filter(is_green_light=False, is_active=True, parent=parent)

        for parent_menu in parent_menus:
            parent_dict = dict()
            parent_dict['id'] = parent_menu.id
            parent_dict['code'] = parent_menu.code
            parent_dict['label'] = parent_menu.name if localization == LANGUAGE_ZH else parent_menu.name_en

            children_menus = Menu.objects.filter(is_green_light=False, is_active=True, parent=parent_menu)

            if children_menus:
                children_list = []
                parent_dict['children'] = children_list
                _run(children_list, parent_menu)
                menu_list.append(parent_dict)
            else:
                nonlocal menu_id_list
                if menu_id_list is None:
                    menu_id_list = MenuSensitiveField.objects.values_list('menu', flat=True).distinct()
                if parent_menu.id in menu_id_list:
                    menu_list.append(parent_dict)

    _run(sensitive_menu_list, sensitive_parent)
